package _021_10_8

func GetNumberOfK(data []int, k int) int {
	// write code here

	left, right := 0, len(data)-1
	index := 0
	count := 0
	for left <= right {
		mid := (left + right) / 2

		if data[mid] == k {
			index = mid
			count++
			break
		} else if data[mid] > k {
			right = mid - 1
		} else {
			left = mid + 1
		}

	}
	for i := index - 1; i >= 0; i-- {
		if data[i] != k {
			break
		}
		count++
	}
	for i := index + 1; i < len(data); i++ {
		if data[i] != k {
			break
		}
		count++
	}
	return count
}
